package com.wyapples.kittcar;

import java.util.ArrayList;
import java.util.regex.Pattern;

import android.R.bool;
import android.R.integer;
import android.R.string;

public class Command {
	// Command Types Definition
	public static final int COMMAND_TYPE_UNKNOW = 0x00;
	public static final int COMMAND_TYPE_CONFIRM = 0x01;
	public static final int COMMAND_TYPE_ACTION = 0x02;
	public static final int COMMAND_TYPE_PARAMETER = 0x03;
	public static final int COMMAND_TYPE_HELLO = 0x04;

	public static final int CONFIRM_TRUE = 0x01;
	public static final int CONFIRM_FALSE = 0x02;

	public static final int ACTION_TOWARD = 0x01;
	public static final int ACTION_BACKWORD = 0x02;
	public static final int ACTION_LEFT = 0x03;
	public static final int ACTION_RIGHT = 0x04;

	public static final String HELLO_WORDS = "hello|hi|kitt|car";

	public static final String CONFIRM_TURE_WORDS = "ok|confirm|yes|yeah";
	public static final String CONFIRM_FALSE_WORDS = "no|negative|deny|not";

	public static final String ACTION_TOWARD_WORDS = "forward|ahead|along|headway|straight";
	public static final String ACTION_BACKWARD_WORDS = "backward|back|return|backtrack";
	public static final String ACTION_LEFT_WORDS = "left";
	public static final String ACTION_RIGHT_WORDS = "right";

	public static final String SPLIT_CHAR_VERTICAL = "\\u007C";
	public static final String SPLIT_CHAR_SPACE = " ";

	private int type, command, parameter;
	private String[] compareWords;
	private String[] recivedWords;

	public Command() {

	}

	public Command(ArrayList<String> recivedStrings) {
		commandAnalyz(recivedStrings);
	}

	public boolean isCompleted() {
		if (type == COMMAND_TYPE_UNKNOW)
			return false;
		else if (type == COMMAND_TYPE_ACTION && parameter == 0)
			return false;

		return true;
	}

	public void commandAnalyz(ArrayList<String> recivedStrings) {
		if (type == COMMAND_TYPE_UNKNOW) {

			// HELLO
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);
				compareWords = HELLO_WORDS.split(SPLIT_CHAR_VERTICAL);

				for (String rWord : recivedWords) {
					for (String cWord : compareWords) {
						if (rWord.equals(cWord)) {
							type = COMMAND_TYPE_HELLO;
							return;
						}
					}
				}
			}

			// CONFIRM_TRUE
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);
				compareWords = CONFIRM_TURE_WORDS.split(SPLIT_CHAR_VERTICAL);

				for (String rWord : recivedWords) {
					for (String cWord : compareWords) {
						if (rWord.equals(cWord)) {
							type = COMMAND_TYPE_CONFIRM;
							command = CONFIRM_TRUE;
							return;
						}
					}
				}
			}

			// CONFIRM_FALSE
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);
				compareWords = CONFIRM_FALSE_WORDS.split(SPLIT_CHAR_VERTICAL);

				for (String rWord : recivedWords) {
					for (String cWord : compareWords) {
						if (rWord.equals(cWord)) {
							type = COMMAND_TYPE_CONFIRM;
							command = CONFIRM_FALSE;
							return;
						}
					}
				}
			}

			// ACTION_TOWARD
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);
				compareWords = ACTION_TOWARD_WORDS.split(SPLIT_CHAR_VERTICAL);

				for (String rWord : recivedWords) {
					if (isNumber(rWord))
						parameter = Integer.parseInt(rWord);
					else {
						for (String cWord : compareWords) {
							if (rWord.equals(cWord)) {
								type = COMMAND_TYPE_ACTION;
								command = ACTION_TOWARD;
							}
						}
					}
				}
			}
			if (type != COMMAND_TYPE_UNKNOW)
				return;

			// ACTION_BACKWORD
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);
				compareWords = ACTION_BACKWARD_WORDS.split(SPLIT_CHAR_VERTICAL);

				for (String rWord : recivedWords) {
					if (isNumber(rWord))
						parameter = Integer.parseInt(rWord);
					else {
						for (String cWord : compareWords) {
							if (rWord.equals(cWord)) {
								type = COMMAND_TYPE_ACTION;
								command = ACTION_BACKWORD;
							}
						}
					}
				}
			}
			if (type != COMMAND_TYPE_UNKNOW)
				return;

			// ACTION_LEFT
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);

				for (String rWord : recivedWords) {
					if (isNumber(rWord))
						parameter = Integer.parseInt(rWord);
					else {
						if (rWord.equals(ACTION_LEFT_WORDS)) {
							type = COMMAND_TYPE_ACTION;
							command = ACTION_LEFT;
						}
					}
				}
			}
			if (type != COMMAND_TYPE_UNKNOW)
				return;

			// ACTION_RIGHT
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);

				for (String rWord : recivedWords) {
					if (isNumber(rWord))
						parameter = Integer.parseInt(rWord);
					else {
						if (rWord.equals(ACTION_RIGHT_WORDS)) {
							type = COMMAND_TYPE_ACTION;
							command = ACTION_RIGHT;
						}
					}
				}
			}
			if (type != COMMAND_TYPE_UNKNOW)
				return;

			// Parameter
			for (String rString : recivedStrings) {
				recivedWords = rString.split(SPLIT_CHAR_SPACE);

				for (String rWord : recivedWords) {
					if (isNumber(rWord)) {
						parameter = Integer.parseInt(rWord);
						type = COMMAND_TYPE_PARAMETER;
					}
				}
			}
			if (type != COMMAND_TYPE_UNKNOW)
				return;
		}
	}

	public boolean isNumber(String str) {
		try {
			Integer.parseInt(str);
			return true;
		} catch (NumberFormatException e) {
			// TODO: handle exception
			return false;
		}
	}

	public int getType() {
		return type;
	}

	public int getCommand() {
		return command;
	}

	public int getParameter() {
		return parameter;
	}

	public void setParameter(int newParameter) {
		parameter = newParameter;
	}
}
